$(document).ready(function(){
	
	//获取物资类型
	$.ajax({
  		type: "post",
 		url: "hmk/getGoodType_getGoodType.action",
  		success:function(data){
  			$.each(data.gtList,function(i,n){
  				$("<option value="+n.gtId+">"+n.gtName+"</option>").appendTo('#gtID');
			});
  		}
	});
	
	//获取物资单位
	$.ajax({
  		type: "post",
 		url: "hmk/getUnit_findAll.action",
  		success:function(data){
  			$.each(data.unitList,function(i,n){
  				$("<option value="+n.unitId+">"+n.unitName+"</option>").appendTo('#goodUnitID');
			});
			/*
			//设置选中的select 			
			var gUnit = $("#gUnitID").text();
			//alert(typeID);
			$("#goodUnitID option").each(function(){
				var v = $(this).attr("value");
				if(v==gUnit){
					$(this).attr("selected","selected");
				}
			});
			*/
  		}
	});
	
	/*
	
		$.ajax({
  		type: "post",
 		url: "hmk/getGoodStatus_findAll.action",
  		success:function(data){
  			$.each(data.gsList,function(i,n){
  				$("<option value="+n.gstatusId+">"+n.gstatusName+"</option>").appendTo('#goodstatusID');
			});
			
			//设置选中的select 			
			var gStatus = $("#gStatusID").text();
			//alert(typeID);
			$("#goodstatusID option").each(function(){
				var v = $(this).attr("value");
				if(v==gStatus){
					$(this).attr("selected","selected");
				}
			});
			
  		}
	});
	
	*/
	
	
	/////////////////////
	$("#acceptForm").validate({
		submitHandler: function(form){   //表单提交句柄,为一回调函数，带一个参数：form  
            //form.submit();   //提交表单
        var gtypeID=$('#gtID option:selected').val();
		var gunitID=$('#goodUnitID option:selected').val();
		var gName=$('#goodNameID').val();
		var gDetail=$('#gdetailID').val();
		
		$.ajax({
  			type: "post",
 			url: "hmk/saveGoods_saveGood.action",
 			data:{
 				  gtypeID1:gtypeID,gunitID1:gunitID,gname1:gName,gdetail1:gDetail
 				 },
  			success:showData
		 });
		  
        },
		rules: {
			'goodNameID':'required',
			'goodUnitID':'required',
			'gtID':'required'
			
		} ,
		messages: {
			'goodNameID':"*必须选",
			'goodUnitID':"*必须选",
			'gtID':"*必须选"
			
		}
	});
	//////////////////////
	
	/*
	$('#ADD_Good').click(function(){
	//alert("add good");
		var gtypeID=$('#gtID option:selected').val();
		var gunitID=$('#goodUnitID option:selected').val();
		var gName=$('#goodNameID').val();
		var gDetail=$('#gdetailID').val();
		
		if(gunitID!==null &&  gunitID.length!==0 && 
		   gName!==null && gName.length!==0  ){
			//gtypeID!==null && gtypeID.length!==0 && 
		 $.ajax({
  			type: "post",
 			url: "hmk/saveGoods_saveGood.action",
 			data:{
 				  gtypeID1:gtypeID,gunitID1:gunitID,gname1:gName,gdetail1:gDetail
 				 },
  			success:showData
		 });
		}else{
			alert("带*号的必填！");
		}
		
	});
	*/
	
	function showData(data){
		if(data.ajaxString=='1'){
			alert("保存成功！");
			window.location.href="/ag/hmk/wuzhiAdd.jsp";
		}else{
			alert("保存失败！");
		}
	}
	
	$("#RESET_G").click(function(){
		window.location.href="/ag/hmk/wuzhiAdd.jsp";
	});

	
});
